﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

namespace Web.Shalo.BanPMS1.Admin
{
    public partial class QLMatHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    THDSLoaiHang();
                    MultiView1.ActiveViewIndex = 0;
                }
            }
        }

        public void THDSLoaiHang()
        {
            LoaiHangController obj = new LoaiHangController();
            GVLoaiHang.DataSource = obj.bllDSLoaiHang();
            GVLoaiHang.DataBind();
        }

        protected void GVLoaiHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
             GVLoaiHang.PageIndex = e.NewPageIndex;
             THDSLoaiHang();
        }

        protected void GVLoaiHang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoaiHangController obj = new LoaiHangController();
            MultiView1.ActiveViewIndex = 1;
            string malh = GVLoaiHang.Rows[e.NewSelectedIndex].Cells[0].Text;
            string tenlh = GVLoaiHang.Rows[e.NewSelectedIndex].Cells[1].Text;
            Session["malh"] = malh;
            Session["tenlh"]=tenlh;
            labTenLH.Text = Session["tenlh"].ToString();
            THDSMatHang();
        }

        public void THDSMatHang()
        {
            MatHangController obj = new MatHangController();
            GVMatHang.DataSource = obj.bllTHDSMatHang(Session["malh"].ToString());
            GVMatHang.DataBind();
        }

        protected void btnTaoMoiMH_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
            btnSuaMH.Visible = false;
            labTenLH2.Text = Session["tenlh"].ToString();
        }

        protected void btnDSLH_Click(object sender, EventArgs e)
        {
            Response.Redirect("QLMatHang.aspx");
        }

        protected void btnThemMH_Click(object sender, EventArgs e)
        {
            MatHangController obj = new MatHangController();
            if (!String.IsNullOrEmpty(txtHinh.FileName))
            {
                string path = Server.MapPath("../Images/" + txtHinh.FileName);
                txtHinh.SaveAs(path);
            }
            long giaban = Convert.ToInt64(txtGiaBan.Text);
            bool res = obj.bllThemMH(txtTenMH.Text, txtHinh.FileName, giaban, txtNhaSX.Text, txtMoTa.Text,Session["malh"].ToString());
            ThongBao(res);
        }

        protected void btnSuaMH_Click(object sender, EventArgs e)
        {
            MatHangController obj = new MatHangController();
            if (!String.IsNullOrEmpty(txtHinh.FileName))
            {
                String patch = "../Images/" + txtHinh.FileName;
                txtHinh.SaveAs(MapPath(patch));
                HidHinh.Value = txtHinh.FileName;

            }
            long giaban = Convert.ToInt64(txtGiaBan.Text);
            bool res = obj.bllSuaMH(txtTenMH.Text, HidHinh.Value, giaban, txtNhaSX.Text, txtMoTa.Text, Session["mamh"].ToString());
            ThongBao(res);
        }

        protected void btnDSMH_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            THDSMatHang();   
        }

        public void ThongBao(bool res)
        {
            if (res)
            {
                Fresh();
                MultiView1.ActiveViewIndex = 1;
                THDSMatHang();

            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }
        }

        protected void GVMatHang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            MatHangController obj = new MatHangController();
            MultiView1.ActiveViewIndex = 2;
            btnThemMH.Visible = false;
            string malh=Session["malh"].ToString();
            string mamh = GVMatHang.Rows[e.NewSelectedIndex].Cells[0].Text;
            Session["mamh"] = mamh;
            labTenLH2.Text = Session["tenlh"].ToString();
            HienThi(mamh);
        }
        public void HienThi(string mamh)
        {
            MatHangController obj=new MatHangController();
            DataTable MH = obj.bllDSMH_MaMH(mamh);
            txtTenMH.Text = MH.Rows[0]["TenMH"].ToString();
            txtGiaBan.Text=MH.Rows[0]["GiaBan"].ToString();
            txtNhaSX.Text = MH.Rows[0]["NhaSX"].ToString();
            txtMoTa.Text=MH.Rows[0]["MoTa"].ToString();
            ImageHinh.ImageUrl="../Images/"+ MH.Rows[0]["Hinh"].ToString();
            HidHinh.Value = MH.Rows[0]["Hinh"].ToString();
        }
        protected void GVMatHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool res = false;
            MatHangController obj = new MatHangController();
            string mamh = GVMatHang.Rows[e.RowIndex].Cells[0].Text;
            res = obj.bllXoaMH(mamh);
            ThongBao(res);
        }

        protected void GVMatHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVMatHang.PageIndex = e.NewPageIndex;
            THDSMatHang();
        }

        public void Fresh()
        {
            txtTenMH.Text = "";
            txtGiaBan.Text = "";
            txtMoTa.Text = "";
            txtNhaSX.Text = "";
         }
    }
}